package cl.mt.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.LinkedList;

import cl.mt.dto.DTOTipoVehiculo;
import cl.mt.util.bd.DatosTabla;
import cl.mt.util.bd.Parametro;

public class DataTipoVehiculo extends DatosTabla {
	private static final String sqlGetId = "SELECT MAX(ID)+1 AS IDX FROM MASTRANSPORTE.MST_VEHICULOS_TIPO";
	private static final String sqlFindByPK = "SELECT ID, DESCRIPCION FROM MASTRANSPORTE.MST_VEHICULOS_TIPO where ID=?";
	private static final String sqlFindAll = "SELECT ID, DESCRIPCION FROM MASTRANSPORTE.MST_VEHICULOS_TIPO";
	private static final String sqlCreate = "INSERT INTO MASTRANSPORTE.MST_VEHICULOS_TIPO (ID, DESCRIPCION) VALUES (?, ?)";
	private static final String sqlDeleteByPK = "DELETE FROM MASTRANSPORTE.MST_VEHICULOS_TIPO where ID=?";
	private static final String sqlUpdate = "UPDATE MASTRANSPORTE.MST_VEHICULOS_TIPO SET DESCRIPCION=? where ID=?";


	private DTOTipoVehiculo setDatosRegistro(ResultSet rs) throws SQLException {
		DTOTipoVehiculo dto = new DTOTipoVehiculo();
		dto.setId(formatInteger(rs.getObject("ID")));
		dto.setDescripcion(formatString(rs.getObject("DESCRIPCION")));
		return dto;
	}

	public Integer generateId() {
		Parametro[] params = null;
		Integer id = null;
		try {
			getConnection();
			ResultSet rs = executeQuery(sqlGetId, params);
			while (rs.next()) {
				id = formatInteger(rs.getObject("IDX"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return id;
	}
	public DTOTipoVehiculo findByPrimaryKey(Long id) {

		DTOTipoVehiculo dto = new DTOTipoVehiculo();
		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindByPK, params);
			while (rs.next()) {
				dto = setDatosRegistro( rs );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return dto;
	}

	public Collection<DTOTipoVehiculo> findAll() {

		Parametro[] params = null;
		Collection<DTOTipoVehiculo> col = new LinkedList<DTOTipoVehiculo>();
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindAll, params);
			while (rs.next()) {
				DTOTipoVehiculo dto = setDatosRegistro( rs );
				col.add( dto );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return col;
	}

	public int deleteByPrimaryKey(Integer id) {

		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlDeleteByPK, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int create(DTOTipoVehiculo dto) {

		Parametro[] params = new Parametro[] {
			getParametro(dto.getId(), Types.INTEGER),
			getParametro(dto.getDescripcion(), Types.VARCHAR)
		};

		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlCreate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int update(DTOTipoVehiculo dto) {
		Parametro[] params = new Parametro[] {
			getParametro(dto.getDescripcion(), Types.VARCHAR),
			getParametro(dto.getId(), Types.INTEGER)
		};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlUpdate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return res;
}
}
